const Vote = require('../model').Vote
const VoteBucket = require('../model').VoteBucket

exports.getAllVoteInTeam = function(teamId, nSkip, nLimit) {
  return Vote.find({ team: teamId })
    .sort({ created_at: -1 })
    .skip(nSkip).limit(nLimit)
    .select('-team -secret')
}

exports.countVotes = function(team) {
  return Vote.count({ team })
}

exports.createVoteInTeam = function(title, type, secret, options, team) {
  return Vote.create({ title, type, secret, options, team })
}

exports.fetchVoteInTeam = function(_id, team) {
  return Vote.findOne({ _id, team })
}

exports.fetchBucket = function(vote, voter) {
  return VoteBucket.findOne({ vote, voter })
}

exports.createBucketForVote = function(vote, voter, data) {
  return VoteBucket.create({ vote, voter, data })
}

exports.fetchBucketForVote = function(vote) {
  return VoteBucket.find({ vote }).select('data -_id')
}

exports.fetchAllBucketsForVote = function(vote) {
  return VoteBucket.find({ vote }).populate({
    path: 'voter',
    select: 'username'
  }).select('-vote -_id')
}
